home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- | Classes:
- | NoodleSlider is a component that expands the functionality of the
- | standard motif slider (they call it 'scale') widget.
- |
- | Author(s) : Paul Isaacs
- |
- */
-
-
-
- #include "NoodleSlider.h"
- #include <X11/StringDefs.h>
- #include <Xm/Form.h>
- #include <Xm/Text.h>
- #include <Xm/Scale.h>
-
- NoodleSlider::NoodleSlider(
- Widget parent,
- const char *name,
- SbBool buildInsideParent,
- int startingMin,
- int startingMax)
- : SoXtComponent(
- parent,
- name,
- buildInsideParent)
- {
- // Build the widget!
- Widget w = buildWidget( getParentWidget(), startingMin, startingMax);
- setBaseWidget(w);
-
- valueChangedCallbacks = new SoCallbackList;
- }
-
- NoodleSlider::~NoodleSlider()
- {
- delete valueChangedCallbacks;
- }
-
- Widget
- NoodleSlider::buildWidget(
- Widget parent,
- int startingMin,
- int startingMax )
- {
- Arg wargs[10];
- int nargs;
-
- _form = XtCreateManagedWidget(getWidgetName(),xmFormWidgetClass,
- parent,NULL, 0);
-
- _value = XtCreateManagedWidget("value",xmTextWidgetClass,_form,NULL,0);
-
- nargs = 0;
- XtSetArg(wargs[nargs],XmNorientation, XmHORIZONTAL ); nargs++;
- XtSetArg(wargs[nargs],XmNdecimalPoints, 2 ); nargs++;
- XtSetArg(wargs[nargs],XmNminimum, startingMin ); nargs++;
- XtSetArg(wargs[nargs],XmNmaximum, startingMax ); nargs++;
- _slider = XtCreateManagedWidget("slider",xmScaleWidgetClass,
- _form,wargs,nargs);
-
- /* set the callbacks for the sub components */
- XtAddCallback( _value, XmNactivateCallback,
- (XtCallbackProc) &NoodleSlider::valueWidgetCallback,
- (XtPointer) this );
- XtAddCallback( _slider, XmNdragCallback,
- (XtCallbackProc) &NoodleSlider::sliderWidgetCallback,
- (XtPointer) this );
- XtAddCallback( _slider, XmNvalueChangedCallback,
- (XtCallbackProc) &NoodleSlider::sliderWidgetCallback,
- (XtPointer) this );
-
- /* take care of layout within the NoodleSlider */
- initLayout();
-
- /* initialize values throughout the NoodleSlider to be those in the scale */
- /* Right now, this winds up filling the label widgets with text */
- setValue( getValue());
- setMin( getMin());
- setMax( getMax());
- return _form;
- }
-
- void
- NoodleSlider::initLayout()
- {
- if (_form == NULL)
- return;
-
- Arg wargs[10];
- int nargs;
- nargs = 0;
- XtSetArg(wargs[nargs],XmNborderWidth, 2 ); nargs++;
- XtSetValues( _form, wargs, nargs );
-
- nargs = 0;
- XtSetArg(wargs[nargs],XmNtopAttachment, XmATTACH_FORM ); nargs++;
- XtSetArg(wargs[nargs],XmNbottomAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNbottomPosition, 100 ); nargs++;
- XtSetArg(wargs[nargs],XmNleftAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNleftPosition, 0 ); nargs++;
- XtSetArg(wargs[nargs],XmNrightAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNrightPosition, 30 ); nargs++;
- XtSetValues( _value, wargs, nargs );
-
- nargs = 0;
- XtSetArg(wargs[nargs],XmNtopAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNtopPosition, 0 ); nargs++;
- XtSetArg(wargs[nargs],XmNbottomAttachment, XmATTACH_FORM ); nargs++;
- XtSetArg(wargs[nargs],XmNleftAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNleftPosition, 30 ); nargs++;
- XtSetArg(wargs[nargs],XmNrightAttachment, XmATTACH_POSITION ); nargs++;
- XtSetArg(wargs[nargs],XmNrightPosition, 100 ); nargs++;
- XtSetArg(wargs[nargs],XmNtitleString, NULL ); nargs++;
- XtSetValues( _slider, wargs, nargs );
- }
-
- void
- NoodleSlider::setValue( float newVal )
- {
- if ( getMin() > newVal )
- setMin( newVal );
- if ( getMax() < newVal )
- setMax( newVal );
-
- /* send the value to the slider */
- if (_slider) {
- int motifIntValue;
- motifIntValue = convertFloatToSlider( newVal );
- XmScaleSetValue( _slider, motifIntValue );
- }
-
- /* send the value to the label */
- char valString[50];
- sprintf( valString, "%.*f", getNumDecimals(), newVal );
- XmTextSetString( _value, valString );
- }
-
- void
- NoodleSlider::setMin( float newVal )
- {
- float valToUse;
-
- if ( newVal <= getValue()
- && newVal < getMax() )
- valToUse = newVal;
- else
- valToUse = getMin();
-
- /* send the new minimum value to the slider */
- if ( _slider == NULL )
- return;
-
- Arg wargs[10];
- int motifIntValue;
-
- motifIntValue = convertFloatToSlider( valToUse );
- XtSetArg( wargs[0], XmNminimum, motifIntValue );
- XtSetValues( _slider, wargs, 1 );
- }
-
- void
- NoodleSlider::setMax( float newVal )
- {
- float valToUse;
-
- if ( newVal >= getValue()
- && newVal > getMin() )
- valToUse = newVal;
- else
- valToUse = getMax();
-
- /* send the new maximum value to the slider */
- if ( _slider == NULL )
- return;
-
- Arg wargs[10];
- int motifIntValue;
-
- motifIntValue = convertFloatToSlider( valToUse );
- XtSetArg( wargs[0], XmNmaximum, motifIntValue );
- XtSetValues( _slider, wargs, 1 );
- }
-
- float
- NoodleSlider::getValue()
- {
- if ( _slider == NULL )
- return 0.0;
-
- Arg wargs[10];
- int motifIntValue;
-
- XtSetArg( wargs[0], XmNvalue, &motifIntValue );
- XtGetValues( _slider, wargs, 1 );
-
- return( convertSliderToFloat( motifIntValue ));
- }
-
- float
- NoodleSlider::getMin()
- {
- if ( _slider == NULL )
- return 0.0;
-
- Arg wargs[10];
- int motifIntValue;
-
- XtSetArg( wargs[0], XmNminimum, &motifIntValue );
- XtGetValues( _slider, wargs, 1 );
-
- return( convertSliderToFloat( motifIntValue ));
- }
-
-
- float
- NoodleSlider::getMax()
- {
- if ( _slider == NULL )
- return 0.0;
-
- Arg wargs[10];
- int motifIntValue;
-
- XtSetArg( wargs[0], XmNmaximum, &motifIntValue );
- XtGetValues( _slider, wargs, 1 );
-
- return( convertSliderToFloat( motifIntValue ));
- }
-
- short
- NoodleSlider::getNumDecimals()
- {
- if (_slider == NULL)
- return 0;
- Arg wargs[10];
- short decimals;
-
- XtSetArg( wargs[0], XmNdecimalPoints, &decimals);
- XtGetValues( _slider, wargs, 1 );
-
- return decimals;
- }
-
- float
- NoodleSlider::convertSliderToFloat( int sliderValue )
- {
- short decimals;
- float theVal;
- int count;
-
- /* shift over the appropriate number of decimal places */
- decimals = getNumDecimals();
- if (decimals < 0)
- decimals = 0;
- for( count = 0, theVal = sliderValue; count < decimals; count++ )
- theVal /= 10.0;
-
- return( theVal );
- }
-
- int
- NoodleSlider::convertFloatToSlider( float floatValue )
- {
- short decimals;
- float convertedFloat;
- int count, returnValue;
-
- /* shift over the appropriate number of decimal places */
- decimals = getNumDecimals();
-
- if (decimals < 0)
- decimals = 0;
- for( count = 0, convertedFloat = floatValue; count < decimals; count++ )
- convertedFloat *= 10.0;
- returnValue = (int) convertedFloat;
- return( returnValue );
- }
-
-
- void
- NoodleSlider::valueWidgetCallback( Widget, void *client_data, void *)
- {
- float theVal;
- NoodleSlider *theNdlSlider = (NoodleSlider *) client_data;
- Arg wargs[10];
- char *valString;
-
- if (theNdlSlider && theNdlSlider->_value ) {
- // get text value from the label
- XtSetArg( wargs[0], XmNvalue, &valString );
- XtGetValues( theNdlSlider->_value, wargs, 1 );
- if ( sscanf( valString, "%f", &theVal ) ) {
- // set the value throughout the NoodleSlider
- theNdlSlider->setValue( theVal );
- }
- theNdlSlider->valueChangedCallbacks->invokeCallbacks(theNdlSlider);
- }
- }
- void
- NoodleSlider::sliderWidgetCallback( Widget, void *client_data, void *)
- {
- float theVal;
- NoodleSlider *theNdlSlider = (NoodleSlider *) client_data;
-
- // Get the value from the slider
- theVal = theNdlSlider->getValue();
-
- theNdlSlider->setValue(theVal);
-
- theNdlSlider->valueChangedCallbacks->invokeCallbacks(theNdlSlider);
- }
-
- void
- NoodleSlider::addValueChangedCallback(SoCallbackListCB *f, void *userData )
- { valueChangedCallbacks->addCallback( f, userData ); }
-
- void
- NoodleSlider::removeValueChangedCallback(SoCallbackListCB *f, void *userData )
- { valueChangedCallbacks->removeCallback( f, userData ); }
-
-
-